<div class="box-body">
    <form class="layui-form" action="{:url('save')}" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label xrequired">渠道</label>
            <div class="layui-input-block">
                <select name="channel_id" lay-search>
                    <option value="">请选择</option>
                    {foreach $channel as $c}
                    <option {if isset($allow) && $allow.channel_id==$c.channel_id}selected{/if} value="{$c.channel_id}">{$c.channel_name}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">可走SKU</label>
            <div class="layui-input-block">
                <div class="allow-select sku" name="include_sku"></div>
                <span class="layui-hide json-data">{if isset($include_sku)}{$include_sku|json_encode}{/if}</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">不可走SKU</label>
            <div class="layui-input-block">
                <div class="allow-select sku" name="exlude_sku"></div>
                <span class="layui-hide json-data">{if isset($exlude_sku)}{$exlude_sku|json_encode}{/if}</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">可走属性</label>
            <div class="layui-input-block">
                <div class="allow-select attr" name="include_attr"></div>
                <span class="layui-hide json-data">{if isset($include_attr)}{$include_attr|json_encode}{/if}</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">不可走属性</label>
            <div class="layui-input-block">
                <div class="allow-select attr" name="exlude_attr"></div>
                <span class="layui-hide json-data">{if isset($exlude_attr)}{$exlude_attr|json_encode}{/if}</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="off" placeholder=""  name="remark" {if isset($allow)}value="{$allow.remark}"{/if}  class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox" name="status" value="1" {if !isset($allow) || (isset($allow) && $allow.status)}checked{/if} lay-skin="switch">
            </div>
        </div>
        {if isset($allow)}
        <input type="hidden" name="allow_id" value="{$allow.allow_id}">
        {/if}
    </form>
</div>
<script type="text/javascript">
    $(".allow-select").each(function()
    {
        var $this = $(this)
            , defVal = []
            , args = {}
            , jsonStr = $this.parent().find(".json-data").text().trim();
        if(jsonStr.length > 0)
        {
            var json = JSON.parse(jsonStr);
            for(let i in json)
            {
                if($this.is(".sku"))
                {
                    defVal.push({
                        "id": {
                            key: "sku",
                            value: json[i].sku
                        },
                        "name": {
                            key: "sku",
                            value: json[i].sku
                        }
                    });
                }
                else if($this.is(".attr"))
                {
                    defVal.push({
                        "id": {
                            key: "attr_id",
                            value: json[i].attr_id
                        },
                        "name": {
                            key: "attr_name",
                            value: json[i].attr_name
                        }
                    });
                }
            }
        }
        args["name"] = $this.attr("name");
        if($this.is(".sku"))
        {
            args["prop"] = {
                "name" : "sku",
                "value": "sku"
            };
            args["select"] = {
                "id" : "sku",
                "name": "sku"
            };
            args["search"] = "sku";
            args["remoteUrl"] = "{:url('wms/product/get')}";

        }
        else if($this.is(".attr"))
        {
            args["prop"] = {
                "name" : "attr_name",
                "value": "attr_id"
            };
            args["select"] = {
                "id" : "attr_id",
                "name": "attr_name"
            };
            args["search"] = "code";
            args["remoteUrl"] = "{:url('cha/attrs/get')}";
        }
        renderSearch(this, args, defVal);
    });
</script>